import requests
from bs4 import BeautifulSoup

from api.login_register import LoginRegister
from config import URLPATH


class Investment:
    # 投资
    def investment(self, session, id, amount, depositCertificate=-1):
        return session.post(url=f'{URLPATH}/trust/trust/tender',
                            headers={'Content-Type': 'application/x-www-form-urlencoded'},
                            data={'id': id, 'depositCertificate': depositCertificate, 'amount': amount})

    # 三⽅投资
    def investment_third(self, session, url, data):
        return session.post(url=url, data=data)

if __name__ == '__main__':
    investment = Investment()
    login = LoginRegister()
    session = requests.session()
    print('-'*200)
    print(login.login_data(session, '18888888888', '123456').text)
    print('-' * 200)
    print(investment.investment(session, '873', '1000').json())
    print('-' * 200)

    # 获取第三方的投资信息,使用BeautifulSoup解析HTML
    html_data = investment.investment(session, '873', '1000').json().get('description').get('form')
    print(html_data)
    soup = BeautifulSoup(html_data, 'html.parser')
    # 获取form标签中method属性的值
    print(soup.form['method'])
    # 获取form标签中action属性的值
    url = soup.form['action']
    print(soup.form['action'])
    # 获取所有input标签,获取name和value属性的值
    json_data = {}
    for i in soup.find_all('input'):
        json_data[i['name']] = i['value']
    print(json_data)
    print(investment.investment_third(session, url, json_data).text)